﻿using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace FileApp
{
    public class APIService
    {
        public static string errMsg { get; set; }



        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static async Task<bool> UploadFile(UploadFileObj request)
        {
            try
            {



                var url = LoginUser.InterfaceUrl + "/Upload/SubmitBase64";
                //using (HttpClient client = new HttpClient())
                {
                    var client = App.httpClientFactory.CreateClient();

                    var json = JsonConvert.SerializeObject(request);

                    StringContent content = new StringContent(json);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                    var resp = await client.PostAsync(url, content);
                    resp.EnsureSuccessStatusCode();
                    var str = await resp.Content.ReadAsStringAsync();
                    var rep = JsonConvert.DeserializeObject<Res<string>>(str);
                    if (rep.code == 1)
                    {
                        return true;
                    }
                    else
                    {
                        errMsg = rep.msg;
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
        }


    }
}
